import std.file, std.string, std.algorithm;
import std.typetuple;

import std.stdio;
static import config;

struct CombatFieldMap
{
	static string output_html_template = 
"<html>
	<body onload='setTimeout(\"location.reload(true);\",4000)'>
	<pre>$map$</pre>
	</body>
</html>";


	char[][] map;
	string legend;

	string cached_map_view;

	void loadFile( in string filename )
	{
		auto s_tmp = readText( filename );

		auto lines = s_tmp.split( "\n" );

		legend = reduce!(" a ~ \"\\n\" ~ b ")( "", until( lines, "" ) );
		
		lines = lines.find( "" );
		lines = lines[1..$-1];

		map.length = lines.length;
	
		foreach ( i, s1; lines )
		{
			auto chars = s1.split;
			map[i].length = chars.length;
			foreach( j, s2; chars )
			{
				map[i][j] = (s2.strip)[0];
			}
		}
	}

	void updateView()
	{
		if ( ! map.length )
			return;

		string tmp = legend ~ "\n\n   ";

		for ( auto i = 0; i < map[0].length; i++ )
		{
			tmp ~= format("%3d",i);
		}

		tmp ~= "\n";

		auto i = 0;
		foreach ( line; map )
		{
			tmp ~= format("%3d",i);
			i++;
			foreach( j, c; line )
			{				
				tmp ~= "  "~c;
			}
			tmp ~= "\n";
		}

		cached_map_view = tmp;
	}

	void saveHTML( string filename )
	{
		std.file.write( filename, output_html_template.replace( "$map$", cached_map_view ) );
	}
}
